<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
      xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head th:include="include :: header"></head>
<body class="hold-transition skin-blue sidebar-mini">
<!--锁屏模态框-->
<div class="modal fade" id="modal-lock" data-backdrop="false" data-keyboard="false">
    <div class="modal-dialog" style="width:100%;height:100%;box-shadow: 0 0px 0px #ccc;">
        <div class="modal-content" style="background: transparent;text-align: center;vertical-align: middle">
            <img th:src="@{/img/loading3.gif}" width="350px" height="350px" style="margin: 0 auto;" />
        </div>
    </div>
</div>
<section class="content-header"><h1>提报计划接收</h1></section>
<section class="content">
    <div class="row">
        <div class="col-xs-12">
            <div class="box">
                <div class="box-header">
                    <form id="queryForm" onsubmit="return false;">
                        <div class="row">
                            <div class="form-inline">
                                <div class="form-group">
                                    <label>项目编号</label>
                                    <input type="text" name="proCode" class="form-control" placeholder="请输入项目编号">
                                </div>
                                <div class="form-group">
                                    <label>项目名称</label>
                                    <input type="text" name="proName" class="form-control" placeholder="请输入项目名称">
                                </div>
                                <div class="form-group">
                                    <label>物料描述</label>
                                    <input type="text" name="malDesc" class="form-control" placeholder="请输入物料描述">
                                </div>
                                <div class="form-group">
                                    <label>提报人</label>
                                    <input type="text" name="postName" class="form-control" placeholder="请输入提报人">
                                </div>
                                <div class="form-group">
                                    <label>采购类型</label>
                                    <select name="reportType" class="form-control">
                                        <option value="3">原材料</option>
                                        <option value="1">外购件</option>
                                        <option value="2">外协件</option>
                                        <option value="" selected="">全部</option>
                                    </select>
                                </div>
                                <div class="form-group">
                                    <label>物料类型</label>
                                    <select name="malType" class="form-control">
                                        <option value="" selected="selected">全部</option>
                                        <option th:each="item : ${vwDictCommonList}" th:value="${item.name}"
                                                th:text="${item.name}"></option>
                                    </select>
                                </div>
                                <div class="form-group">
                                    <label for="select_recMark">接收状态</label>
                                    <select name="recMark" id="select_recMark" class="form-control">
                                        <option value="0" selected="selected">未接收</option>
                                        <option value="1">已接收</option>
                                    </select>
                                </div>
                                <div class="form-group">
                                    <label>创建时间</label>
                                    <input type="text" class="form-control datepicker" name="startTime"
                                           readonly="readonly"/>
                                    <span> 至 </span>
                                    <input type="text" class="form-control datepicker" name="endTime"
                                           readonly="readonly"/>
                                </div>
                                <div class="form-group">
                                    <label>接收时间</label>
                                    <input type="text" class="form-control datepicker" name="recStartTime"
                                           readonly="readonly"/>
                                    <span> 至 </span>
                                    <input type="text" class="form-control datepicker" name="recEndTime"
                                           readonly="readonly"/>
                                </div>
                                <input type="hidden" class="form-control" id = "exportIds" name="exportIds"/>
                                <button type="button" class="btn btn-success" id="btn-search">
                                    <span class="Bold">搜索</span>
                                </button>
                            </div>

                        </div>
                    </form>
                    <div class="row" id="toolbar">
                        <div class="form-inline">
                            <img th:src="@{/img/downloadding.gif}" id="btn_tips2" style="display: none"/>
                            <button id = "button_exportSelect" shiro:hasPermission="buy:planreceive:export" class="btn btn-warning pull-right" style="margin-right: 10px;display: none"
                                    onclick="exportSelectData(this)">
                                <span class="Bold">导出选中</span>
                            </button>
                            <img th:src="@{/img/downloadding.gif}" id="btn_tips1" style="display: none"/>
                            <button id = "button_exportAll" shiro:hasPermission="buy:planreceive:export" class="btn btn-warning pull-right" style="margin-right: 10px;display: none"
                                    onclick="exportAllData(this)">
                                <span class="Bold">导出全部</span>
                            </button>
                            <button id="button_batchReceive"
                                    shiro:hasPermission="buy:planreceive:edit"
                                    class="btn btn-success pull-right"
                                    style="margin-right: 10px"
                                    onclick="receiveAll()">
                                <span class="Bold">批量接收</span>
                            </button>
                        </div>
                    </div>
                </div>
                <div class="box-body">
                    <table id="bootstrap-table"></table>
                    <iframe id="previewpdf" src="" width="100%" frameborder="0"></iframe>
                </div>
            </div>
        </div>
    </div>
</section>
<div th:include="include :: footer"></div>
<script th:inline="javascript">
    let imageFileType = ['BMP','JPG','PNG','TIF','GIF','PCX','TGA','EXIF','FPX','SVG','PSD','CDR','PCD','DXF','UFO','EPS','AI','RAW','WMF','WEBP'];
    /** 表格 */
    let table;

    /**
     * 根据搜索条件导出全部数据
     */
    function exportAllData(btn){
        $("#exportIds").val(null);
        exportExcel(btn,$("#btn_tips1"));
    }
    function exportSelectData(btn){
        const selectedRows = $('#bootstrap-table').bootstrapTable('getSelections');
        if (selectedRows.length === 0) {
            alert('请先选择数据');
            return;
        }
        // 2. 提取选中行的唯一ID（假设ID字段为'id'）
        const selectedIds = selectedRows.map(row => row.id);
        $("#exportIds").val(selectedIds.join(','));
        exportExcel(btn,$("#btn_tips1"));
    }
    function exportExcel(btn,btnTrips){
        $(btn).hide();
        $(btnTrips).show();
        // 1. 发起GET请求，触发Excel下载
        window.location.href = "/buy/getReportPlanList/exportExcelAllData" +"?" + $("#queryForm").serialize();;
        js.modal.success('正在执行导出Excel任务，请稍等片刻！');
        setTimeout(() => {
            $(btnTrips).hide();
            $(btn).show();
        }, 5000); // 2秒后自动关闭加载态（可根据实际调整）
    }
    $(function () {
        $("#select_recMark").on('change',function (e) {
            if($("#select_recMark").val() === '1'){
                $("#button_exportAll").show();
                $("#button_exportSelect").show();
                $("#button_batchReceive").hide();
            }else if($("#select_recMark").val() === '0'){
                $("#button_exportAll").hide();
                $("#button_exportSelect").hide();
                $("#button_batchReceive").show();
            }
            else{
                $("#button_exportAll").hide();
                $("#button_exportSelect").hide();
                $("#button_batchReceive").hide();
            }
        });
        $("title").html($("title").html()+" —— 提报计划接收");
        /** 初始化表格 */
        table = js.table.init({
            url: ctx + "buy/getReportPlanList/receive/list",
            showColumns: false,
            showExport: false,
            pageSize: 300,
            pageList: [10, 15, 25, 50, 100, 300, 500, 1000],
            uniqueId: 'id',
            paginationVAlign: 'both',
            onLoadSuccess: function (){
                $('#btn-search').prop('disabled', false).removeClass('disabled');
            },
            columns: [
                {
                    checkbox: true,
                    width: '30px',
                    // formatter: function (value, row, index) {
                    //     // 正常提报并且数据正常状态可以提交选择
                    //     if (row.recMark === 0) {
                    //         return {disabled: false}
                    //     } else {
                    //         return {disabled: true}
                    //     }
                    // }
                },
                {
                    title: '序号', field: 'id', width: '50px',
                    formatter: function (value, row, index, field) {
                        return index + 1;
                    }
                },
                {title: "项目编号", field: 'proCode'},
                {title: "项目名称", field: 'proName'},
                {title: "物料描述", field: 'malDesc'},
                {title: "数量", field: 'displayNum',
                    formatter: function (value, row, index) {
                        return value;
                    }
                },
                {title: "单位", field: 'unit'},
                {
                    title: "采购类型", field: 'reportType',
                    formatter: function (value, row, index) {
                        if (value === 1) {
                            return "外购件";
                        } else if (value === 2) {
                            return "外协件";
                        } else {
                            return "原材料";
                        }
                    }
                },
                {title: "物料类型", field: 'malType'},
                {
                    title: "部分", field: 'part',width: '70'
                    ,formatter: function (value, row, index) {
                        if (value != null) {
                            return '<p style="word-wrap: break-word;width: 60px">' + value + '</p>';
                        }  else {
                            return "--";
                        }
                    }
                },
                {
                    title: "型号/标准号", field: 'standard',width: '85'
                    ,formatter: function (value, row, index) {
                        if (value != null) {
                            return '<p style="word-wrap: break-word;width: 80px">' + value + '</p>';
                        }  else {
                            return "--";
                        }
                    }
                },
                {
                    title: "品牌", field: 'brand'
                    ,formatter: function (value, row, index) {
                        if (value != null) {
                            return value;
                        }  else {
                            return "--";
                        }
                    }
                },
                {
                    title: "备注", field: 'remark',
                    formatter: function (value, row, index) {
                        if (value != null) {
                            return value;
                        }  else {
                            return "--";
                        }
                    }
                },
                {
                    title: "是否使<br>用库存", field: 'useStore',
                    formatter: function (value, row, index) {
                        if(value === null){
                            return "--";
                        }
                        if (value === 0) {
                            return "否";
                        } else {
                            return "是";
                        }
                    }
                },
                {
                    title: "图号", field: 'diagram'
                    ,formatter: function (value, row, index) {
                        if (value != null) {
                            return value;
                        }  else {
                            return "--";
                        }
                    }
                },
                {
                    title: "外协类型", field: 'assistType'
                    ,formatter: function (value, row, index) {
                        if (value != null) {
                            return value;
                        }  else {
                            return "--";
                        }
                    }
                },
                {title: "提报人", field: 'postName'},
                {title: "联系方式", field: 'postTel'},
                {title: "提报人<br>部门", field: 'branchName'},
                {title: "下达时间", field: 'lssueTime', width: '90px'},
                {title: "接收时间", field: 'recTime', width: '90px'},
                {
                    title: "附件", field: 'extendUrl', formatter: function (value, row, index) {
                        if(value!=null){
                            let elements = [];
                            if(hasP('buy:planreceive:edit')){
                                elements.push('<a class="btn btn-info btn-xs" href="javascript:void(0)" onclick="look(' + row.id + ',' + row.recMark + ')"><i class="fa  fa-search"></i> 查看</a> ');
                            }
                            if(hasP('buy:planreceive:edit')){
                                elements.push('<a class="btn btn-primary btn-xs" href="/buy/getReportPlanList/download/' + row.id + '/' + row.recMark + '"><i class="glyphicon glyphicon-download-alt"></i> 下载</a> ');
                            }
                            return elements.join('');
                        }else {
                            return "";
                        }
                    }
                },
                {
                    title: "操作",
                    width: '110px',
                    formatter: function (value, row, index) {
                        let actions = [];
                            //未接收
                            if (row.recMark === 0 && hasP('buy:planreceive:edit')) {
                                actions.push('<button class="btn btn-success btn-xs" onclick="receive(' + row.id + ')"> 接收</button> ');
                            }
                           // 未接收
                           //  if(hasP('buy:planreceive:return') && row.recMark === 0){
                           //      actions.push('<button class="btn btn-warning btn-xs" onclick="returnPlan(' + row.id + ')"> 退回</button> ');
                           //  }
                        return actions.join('');
                    }
                }
            ]
        });
        /** 表格搜索 */
        $("#btn-search").click(function () {
            $('#btn-search').prop('disabled', true).addClass('disabled');
            js.table.search(table);
        });
    });

    // /**
    //  * 退回计划
    //  * */
    // function returnPlan(id) {
    //     js.modal.confirm("确认要退回此计划吗？",function (){
    //         js.modal.open("modal-lock");
    //         js.post({
    //             url: ctx + "buy/getReportPlanList/return/plan",
    //             data: {
    //                 id:id
    //             },
    //             success: function (result) {
    //                 if(result.type === web_status.SUCCESS){
    //                     js.modal.hide("modal-lock");
    //                     js.table.refresh(table);
    //                 }else {
    //                     js.modal.hide("modal-lock");
    //                     js.modal.warning(result.msg);
    //                     js.table.refresh(table);
    //                 }
    //             }
    //         })
    //     })
    //
    // }

    /**
     * 查看
     * */
    function look(id, recMark) {
        js.post({
            url: ctx + "buy/getReportPlanList/lookFile",
            data: {
                id: id,
                recMark: recMark
            },
            success: function (result) {
                if (result.type === web_status.SUCCESS) {
                    let fileExtensions = result.data.substring(result.data.lastIndexOf(".")+1);
                    if (fileExtensions.toUpperCase()==='PDF') {
                        window.open(result.data);
                        // window.open([[@{/js/pdf/web/viewer.html}]]+'?file=' + result.data, 'PDF');
                    }else {
                        let isOPen=false;
                        for (let i = 0; i < imageFileType.length; i++) {
                            fileExtensions=fileExtensions.toUpperCase();
                            if (fileExtensions==imageFileType[i])  {
                                isOPen=true;
                                window.open(result.data);
                                break;
                            }
                        }
                        if(isOPen===false){
                            js.modal.warning("该文件格式是："+fileExtensions+"文件无法预览!");
                        }
                    }
                } else {
                    js.modal.warning("文件损坏！");
                }
            }
        })


    }

    /**
     * 接收提报计划
     */
    function receive(ids) {
        js.post({
            url: ctx + "buy/getReportPlanList/receive/plans",
            data: {
                ids: ids
            },
            success: function (result) {
                if (result.type === web_status.SUCCESS) {
                    js.modal.success(result.msg);
                    js.table.refresh(table);
                } else {
                    js.modal.warning(result.msg);
                }
            }
        })
    }

    /**
     * 批量接收提报计划
     */
    function receiveAll() {
        let arr = js.table.selectColumns(table, "id");
        if (arr.length < 1) {
            js.modal.alert("请选择要提交的计划");
            return;
        } else {
            let ids = "";
            for (let i = 0; i < arr.length; i++) {
                ids += arr[i] + ",";
            }
            ids = ids.substring(0, ids.length - 1);
            receive(ids);
        }
    }
</script>
</body>
</html>
